﻿#include "Behemoth.h"
#include "../../../Shared/Functions/Functions.h"
#include "../../MirEnvir/Envir.h"
#include "../../../Shared/ServerPackets.h"
#include "../../../Shared/Data/Stat.h"
#include "../DelayedAction.h"
#include "../../../Shared/Enums.h"
#include "../MapObject.h"
#include "../../MirEnvir/Map.h"
#include "../../Settings.h"

using namespace Server::MirDatabase;
using namespace Server::MirEnvir;
namespace S = ServerPackets;

namespace Server::MirObjects::Monsters
{

    Behemoth::Behemoth(MonsterInfo *info) : MonsterObject(info)
    {
    }

    bool Behemoth::InAttackRange()
    {
        return getCurrentMap() == getTarget()->getCurrentMap() && Functions::InRange(getCurrentLocation(), getTarget()->getCurrentLocation(), AttackRange);
    }

    void Behemoth::Attack()
    {
        if (!getTarget()->IsAttackTarget(this))
        {
            setTarget(nullptr);
            return;
        }

        setDirection(Functions::DirectionFromPoint(getCurrentLocation(), getTarget()->getCurrentLocation()));
        bool ranged = getCurrentLocation() == getTarget()->getCurrentLocation() || !Functions::InRange(getCurrentLocation(), getTarget()->getCurrentLocation(), 1);

        if (!ranged)
        {
            switch (getEnvir()->Random->Next(5))
            {
                case 0:
                case 1:
                case 2:
                    MonsterObject::Attack(); //swipe
                    break;
                case 3:
                {
                        S::ObjectAttack *tempVar = new S::ObjectAttack();
                        tempVar->ObjectID = ObjectID;
                        tempVar->Direction = getDirection();
                        tempVar->Location = getCurrentLocation();
                        tempVar->Type = 1;
                        Broadcast(tempVar);

                        int damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC]);
                        if (damage == 0)
                        {
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
                            return;
                        }

                        DelayedAction *action = new DelayedAction(DelayedType::Damage, getEnvir()->getTime() + 300, {getTarget(), damage, DefenceType::ACAgility, true});
                        ActionList.push_back(action);

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
                }
                    break;
                case 4:
                {
                        S::ObjectAttack *tempVar2 = new S::ObjectAttack();
                        tempVar2->ObjectID = ObjectID;
                        tempVar2->Direction = getDirection();
                        tempVar2->Location = getCurrentLocation();
                        tempVar2->Type = 2;
                        Broadcast(tempVar2);

                        DelayedAction *action = new DelayedAction(DelayedType::Damage, getEnvir()->getTime() + 300, {getTarget(), 0, DefenceType::ACAgility, false});
                        ActionList.push_back(action);

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar2' statement was not added since tempVar2 was passed to a method or constructor. Handle memory management manually.
                }
                    break;
            }

            PoisonTarget(getTarget(), 15, 5, PoisonType::Bleeding);
        }
        else
        {
            if (getEnvir()->Random->Next(2) == 0)
            {
                MoveTo(getTarget()->getCurrentLocation());
            }
            else
            {
                switch (getEnvir()->Random->Next(2))
                {
                    case 0:
                        S::ObjectRangeAttack *tempVar3 = new S::ObjectRangeAttack();
                        tempVar3->ObjectID = ObjectID;
                        tempVar3->Direction = getDirection();
                        tempVar3->Location = getCurrentLocation();
                        tempVar3->Type = 0;
                        Broadcast(tempVar3);

                        SpawnSlaves(); //spawn huggers
                        break;
                    case 1:
                    {
                            S::ObjectRangeAttack *tempVar4 = new S::ObjectRangeAttack();
                            tempVar4->ObjectID = ObjectID;
                            tempVar4->Direction = getDirection();
                            tempVar4->Location = getCurrentLocation();
                            tempVar4->Type = 1;
                            Broadcast(tempVar4);

                            int damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC]) * 3;
                            if (damage == 0)
                            {
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar4' statement was not added since tempVar4 was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar3' statement was not added since tempVar3 was passed to a method or constructor. Handle memory management manually.
                                return;
                            }

                            DelayedAction *action = new DelayedAction(DelayedType::RangeDamage, getEnvir()->getTime() + 500, {getTarget(), damage, DefenceType::ACAgility});
                            ActionList.push_back(action);


//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar4' statement was not added since tempVar4 was passed to a method or constructor. Handle memory management manually.
                    }
                        break;

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar3' statement was not added since tempVar3 was passed to a method or constructor. Handle memory management manually.
                }
            }

            ShockTime = 0;
            ActionTime = getEnvir()->getTime() + 300;
            AttackTime = getEnvir()->getTime() + AttackSpeed;
        }
    }

    void Behemoth::CompleteRangeAttack(std::vector<std::any> &data)
    {
        MapObject *target = std::any_cast<MapObject*>(data[0]);
        int damage = std::any_cast<int>(data[1]);
        DefenceType defence = std::any_cast<DefenceType>(data[2]);

        if (target == nullptr || !target->IsAttackTarget(this) || target->getCurrentMap() != getCurrentMap() || target->Node == nullptr)
        {
            return;
        }

        std::vector<MapObject*> targets = FindAllTargets(AttackRange, getCurrentLocation());
        if (targets.empty())
        {
            return;
        }

        for (int i = 0; i < targets.size(); i++)
        {
            S::ObjectEffect *tempVar = new S::ObjectEffect();
            tempVar->ObjectID = targets[i]->ObjectID;
            tempVar->Effect = SpellEffect::Behemoth;
            Broadcast(tempVar);

            if (targets[i]->Attacked(this, damage, DefenceType::ACAgility) <= 0)
            {
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
                continue;
            }

            PoisonTarget(targets[i], 15, 5, PoisonType::Paralysis, 1000);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
        }
    }

    void Behemoth::CompleteAttack(std::vector<std::any> &data)
    {
        MapObject *target = std::any_cast<MapObject*>(data[0]);
        int damage = std::any_cast<int>(data[1]);
        DefenceType defence = std::any_cast<DefenceType>(data[2]);
        bool fireCircle = std::any_cast<bool>(data[3]);

        if (target == nullptr || !target->IsAttackTarget(this) || target->getCurrentMap() != getCurrentMap() || target->Node == nullptr)
        {
            return;
        }

        if (fireCircle) //Firecircle
        {
            std::vector<MapObject*> targets = FindAllTargets(1, getCurrentLocation());

            if (targets.empty())
            {
                return;
            }

            for (int i = 0; i < targets.size(); i++)
            {
                targets[i]->Attacked(this, damage, DefenceType::AC);
            }
        }
        else //Push back
        {
            Point *point = Functions::PointMove(getCurrentLocation(), getDirection(), 1);

            Cell *cell = getCurrentMap()->GetCell(point);

            if (!cell->Objects.empty())
            {
                for (int o = 0; o < cell->Objects.size(); o++)
                {
                    MapObject *t = cell->Objects[o];
                    if (t == nullptr || t->getRace() != ObjectType::Player)
                    {
                        continue;
                    }

                    if (t->IsAttackTarget(this))
                    {
                        t->Pushed(this, getDirection(), 4);

                        PoisonTarget(t, 3, 15, PoisonType::Dazed, 1000);
                    }
                    break;
                }
            }
        }
    }

    void Behemoth::SpawnSlaves()
    {
        std::vector<MapObject*> targets = FindAllTargets(10, getCurrentLocation());

        int count = std::min(8, (targets.size() * 5) - SlaveList.size());

        ActionTime = getEnvir()->getTime() + 300;
        AttackTime = getEnvir()->getTime() + AttackSpeed;

        for (int i = 0; i < count; i++)
        {
            MonsterObject *mob = nullptr;
            switch (getEnvir()->Random->Next(4))
            {
                case 0:
                    mob = GetMonster(getEnvir()->GetMonsterInfo(Settings::BehemothMonster1));
                    break;
                case 1:
                    mob = GetMonster(getEnvir()->GetMonsterInfo(Settings::BehemothMonster2));
                    break;
                case 2:
                    mob = GetMonster(getEnvir()->GetMonsterInfo(Settings::BehemothMonster3));
                    break;
            }

            if (mob == nullptr)
            {
                continue;
            }

            if (!mob->Spawn(getCurrentMap(), getFront()))
            {
                mob->Spawn(getCurrentMap(), getCurrentLocation());
            }

            mob->setTarget(targets[getEnvir()->Random->Next(targets.size())]);
            mob->ActionTime = getEnvir()->getTime() + 2000;
            SlaveList.push_back(mob);
        }
    }

    void Behemoth::Die()
    {
        for (auto slave : SlaveList)
        {
            slave->Die();
        }

        MonsterObject::Die();
    }
}
